package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        // 思路： 看当前当前用户， 是否添加过购物车，如果有，则更新份数，如果没有，添加到数据库的表中
        // 1. 查询数据库表，判断当前用户是否有购物记录
        // 把DTO转实体对象
        ShoppingCart queryShoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,queryShoppingCart);
        queryShoppingCart.setUserId(BaseContext.getCurrentId());
        // userId,dishId/setmealId, flaors
        log.info("[添加购物车] shoppingCart:{}",queryShoppingCart);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByAll(queryShoppingCart);
        ShoppingCart dbShoppingCart = null;
        // 2. 如果有，更新购买的数量
        if(shoppingCartList != null && shoppingCartList.size()>0){
            dbShoppingCart = shoppingCartList.get(0);
            dbShoppingCart.setNumber(dbShoppingCart.getNumber()+1);
            //  根据这条记录的主键，去更新记录，让数量+1
            shoppingCartMapper.updateShopingCartById(dbShoppingCart);
        }else {
            // 3. 如果没用记录，添加记录
            // 需要判断当前是菜品还是套餐
            Long dishId = queryShoppingCart.getDishId();
            if (dishId != null){
                // 当前是菜品，根据菜品ID，查询菜品数据
                Dish dish = dishMapper.selectById(dishId);
                queryShoppingCart.setName(dish.getName());
                queryShoppingCart.setAmount(dish.getPrice());
                queryShoppingCart.setImage(dish.getImage());
            }else{
                // 当前是套餐,根据套餐ID，查询套餐数据
               Setmeal setmeal =  setmealMapper.selectById(queryShoppingCart.getSetmealId());
                queryShoppingCart.setName(setmeal.getName());
                queryShoppingCart.setAmount(setmeal.getPrice());
                queryShoppingCart.setImage(setmeal.getImage());
            }
            // 数量都是1，时间当前
            queryShoppingCart.setNumber(1);
            queryShoppingCart.setCreateTime(LocalDateTime.now());
            log.info("[添加购物记录] shoppingCart:{}",queryShoppingCart);
            shoppingCartMapper.insertShoppingCart(queryShoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> findCartList() {
        // 构建查询条件
        ShoppingCart queryShopingCart = new ShoppingCart();
        queryShopingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.selectByAll(queryShopingCart);
    }

    @Override
    public void cleanCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
